package com.elf.zero.form;

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.elf.zero.app.AbsLinearLayout;

import java.util.List;

/**
 * RadioButtonGroup
 */
public class FormRadioButtonGroup extends AbsLinearLayout {

    protected TextView tv_text,tv_must;
    protected RadioGroup radio_group;

    protected List<FormOption> options;
    private OnOptionSelectedListener onOptionSelectedListener;

    public FormRadioButtonGroup(Context context) {
        super(context);
    }

    public FormRadioButtonGroup(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    protected int getLayoutResId() {
        return R.layout.zero_form_radio_button_group;
    }

    @Override
    protected void initView(AttributeSet attrs) {
        tv_must = findViewById(R.id.tv_must);
        tv_text = findViewById(R.id.tv_text);
        radio_group = findViewById(R.id.radio_group);

        if (attrs != null) {
            TypedArray typedArray = getContext().obtainStyledAttributes(attrs, R.styleable.FormRadioButtonGroup);
            tv_text.setText(typedArray.getString(R.styleable.FormRadioButtonGroup_form_text));
            boolean mustShow = typedArray.getBoolean(R.styleable.FormRadioButtonGroup_form_must, true);
            tv_must.setVisibility(mustShow ? VISIBLE : GONE);
            typedArray.recycle();
        }
    }

    public void setOptions(List<FormOption> options) {
        radio_group.removeAllViews();
        this.options = options;

        if (options.isEmpty()) {
            return;
        }

        for (int i = 0; i < options.size(); i++) {
            FormOption option = options.get(i);
            RadioButton radioButton = new RadioButton(getContext());
            radioButton.setTag(option);
            radioButton.setText(option.name);
            radioButton.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (onOptionSelectedListener != null) {
                        onOptionSelectedListener.selected(getSelectedItem());
                    }
                }
            });
            radio_group.addView(radioButton,new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
        }
    }

    public FormOption getSelectedItem() {
        for (int i = 0; i < radio_group.getChildCount(); i++) {
            RadioButton radioButton = (RadioButton) radio_group.getChildAt(i);
            if (radioButton.isChecked()) {
                return (FormOption) radioButton.getTag();
            }
        }
        return null;
    }

    public void setOnOptionSelectedListener(OnOptionSelectedListener listener) {
        onOptionSelectedListener = listener;
    }

    public void setMust(boolean b) {
        tv_must.setVisibility(b ? VISIBLE : GONE);
    }
}
